import { Dayjs } from '@shencom/utils-date';
import {
  CreateDateColumn,
  DeleteDateColumn,
  UpdateDateColumn,
  ValueTransformer,
} from 'typeorm';

export const dataTransformer: ValueTransformer = {
  // 存
  to(value: Date | string | number) {
    return value ? Dayjs(value).toDate() : undefined;
  },
  // 取
  from(value: Date) {
    return value ? Dayjs(value).valueOf() : undefined;
  },
};
export class DateBaseEntity {
  @CreateDateColumn({ transformer: dataTransformer })
  createdAt: Date;

  @UpdateDateColumn({ transformer: dataTransformer })
  updatedAt: Date;

  @DeleteDateColumn({ transformer: dataTransformer })
  deletedAt: Date;
}
